{extend name='layout:app'}

{block name='css'}
<link rel="stylesheet" href="__STATIC__/css/addr-list.css">
{/block}

{block name='content'}
<div id="app" v-loading="loading">
    <div class="nav">
        <i class="el-icon-arrow-left icon_left" @click="back"></i>
        <div>收寄地址</div>
        <i class="iconfont icon-home icon_right" @click="home"></i>
    </div>

    <div class="addr-box">
        <div class="addr-item" v-for="(item,index) in addrList">
            <div class="item-top" style="color: #a29c9c;">
                <div>
                    <span class="name">{{item.name}}</span>
                    <span class="phone">{{item.phone}}</span>
                </div>
                <div class="addr">{{item.addr}}</div>
            </div>
            <div class="item-bot">

                <span class="set" :class="item.addr_default==='默认地址' ? 'activeColor' : ''" @click="setDefaultAddr($event,index,item)">
                    <i class="el-icon-circle-check"></i>{{item.addr_default}}
                </span>
                <span class="flex40" @click="edit(item)">编辑</span>
                <span class="flex40" @click="del(item)">删除</span>
            </div>
        </div>
    </div>
    <div class="add-newAddr"  v-show="addrList.length !== 0">
        <el-button type="primary" @click="addNewAddr"><i class="el-icon-plus"></i>添加新地址</el-button>
    </div>


    <div class="btn-area">
        <div class="order">
            <el-button type="primary" @click="chooseOk">确认</el-button>
        </div>
    </div>
</div>
{/block}

{block name='footer'}{/block}

{block name='js'}
<script>
    var that
    new Vue({
        el:'#app',
        data: {
            loading: true,
            addrList: [],
            defaultAddr: {},
            changedDefault: false
        },
        created() {
            that = this
            // $('#modal').height(window.screen.height-70)
            /*var _addrList = JSON.parse(localStorage.getItem('addrList'))
            if(_addrList) {
                this.addrList = _addrList
            }

            var uInfo = JSON.parse(localStorage.getItem('uInfo'))
            if(uInfo) {
                this.addrList.push({name: uInfo.name,phone: uInfo.phone,addr: uInfo.addr})
                localStorage.removeItem('uInfo')
            }
            localStorage.setItem('addrList',JSON.stringify(this.addrList))*/
            /*if(localStorage.getItem('clickType') == 1) {
                localStorage.setItem('addrList',JSON.stringify(this.addrList))
            }*/
            $('#app').height(window.innerHeight)
            var _sAddr = JSON.parse(localStorage.getItem('_SendAddr')),
                _rAddr = JSON.parse(localStorage.getItem('_ReceiveAddr'))
            const _clickType = sessionStorage.getItem('clickType')

            if(_sAddr && (_clickType == 0 || _clickType == 2)) {
                this.defaultAddr = _sAddr
            }
            if(_rAddr && _clickType == 1) {
                this.defaultAddr = _rAddr
            }
            const _addrList =  _clickType == 0 || _clickType == 2 ? '/index/person/getaddr?type=0' : '/index/person/getaddr?type=1'
            $.get(_addrList,function (res) {
                console.log(res)
                if(res.code === 0) {
                    for(item of res.data) {
                        that.addrList.push({id:item.addr_id,name: item.addr_name,phone: item.addr_phone,
                            addr_default:item.addr_default === 1 ? '默认地址' : '设为默认地址',
                            addr: `${item.addr_province} ${item.addr_city} ${item.addr_area} ${item.addr_detail}`})
                    }
                    that.loading = false
                }
            })
        },
        mounted() {
            //  默认地址样式
            var hasDefault = JSON.parse(localStorage.getItem('defaultAddr'))
            if(hasDefault) {
                console.log($(`.addr-item:eq(${hasDefault.index})`))
                $(`.addr-item:eq(${hasDefault.index}) .set`).html('<i class="el-icon-circle-check"></i>默认地址')
                $(`.addr-item:eq(${hasDefault.index}) .set`).addClass('activeColor')
            }
        },
        methods: {
            back() {
                window.history.go(-1)
            },
            home() {
                location.href = '/'
            },
            setDefaultAddr(e,i,info) {
                if($(e.target).hasClass('activeColor')) return
                $(e.currentTarget).html('<i class="el-icon-circle-check activeColor"></i>默认地址')
                $(e.currentTarget).addClass('activeColor')
                $(e.currentTarget).parents('.addr-item').siblings('.addr-item').find('.set').removeClass('activeColor')
                $(e.currentTarget).parents('.addr-item').siblings('.addr-item').find('.set').html('<i class="el-icon-circle-check"></i>设为默认地址')
                /*console.log(i)
                var _defaultAddrInfo = {
                    index: i,
                    name: info.name,
                    phone: info.phone,
                    addr: info.addr
                }
                localStorage.setItem('defaultAddr',JSON.stringify(_defaultAddrInfo))*/
                this.defaultAddr = {   //默认地址信息
                    index: i,
                    id: info.id,
                    name: info.name,
                    phone: info.phone,
                    addr: info.addr
                }
                this.changedDefault = true
            },
            edit(item) {
                console.log(item)
                sessionStorage.setItem('addr',JSON.stringify(item))
                window.location.href = '/index/person/addredit.html'
            },
            del(item) {
                this.$confirm('确认删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    $.post('/index/person/deladdr',{id: item.id},function (res) {
                        if(res.code === 0) {
                            that.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            setTimeout(() =>{
                                location.reload()
                            }, 1000)
                        }
                    })
                }).catch(() => {

                });
            },
            addNewAddr() {
                /*var _comeFrom = location.href.split('?')[1]
                _comeFrom ? location.href = 'add_new_addr.html'+'?'+_comeFrom : location.href = 'add_new_addr.html'*/
                location.href = '/index/person/addNewAddr'
            },
            chooseOk() {
                /*if(!JSON.parse(localStorage.getItem('defaultAddr'))) {
                   alert('请选择一个默认地址')
                   return
               }
               /*iocument.referrer.includes('send-by2') || location.href.includes('comefrom=sendBy2')) {
                   location.href = 'send-by2.html'
               }else {
                   if(sessionStorage.getItem('send-type') == 1) {
                       location.href = 'send.html?send_way=1'
                   }else {
                       location.href = 'send.html'
                   }
               }*/
                if(!$('.addr-item .activeColor').length) {
                    alert('请选择一个默认地址')
                    return
                }

                const _clickType = sessionStorage.getItem('clickType')
                const _service = sessionStorage.getItem('service')
                if(_clickType == 0 || _clickType == 2) {
                    localStorage.setItem('_SendAddr',JSON.stringify(that.defaultAddr))
                }else {
                    localStorage.setItem('_ReceiveAddr',JSON.stringify(that.defaultAddr))
                }

                if(!this.changedDefault) {
                    if(_service == 'onsite') {
                        location.href = '/index/services/onsiteService'
                        return
                    }
                    if(sessionStorage.getItem('send-type') == 1) {
                        location.href = '/index/services/send?send_way=1'
                    }else {
                        location.href = '/index/services/send'
                    }
                    return
                }
                $.post('/index/person/addrdefault',{addr_id:that.defaultAddr.id,addr_type:sessionStorage.getItem('clickType') == 1 ? 1 : 0},function (res) {
                    if(res.code === 0) {
                        if(_clickType == 1 && _service == 'onsite') {
                            location.href = '/index/services/onsiteService'
                            return
                        }
                        if(sessionStorage.getItem('send-type') == 1) {
                            location.href = '/index/services/send?send_way=1'
                        }else {
                            location.href = '/index/services/send'
                        }
                    }
                })
            }
        }
    });

</script>
{/block}